package com.project.movie.controller.seat;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.movie.common.enums.ResultCode;
import com.project.movie.domain.MovieSeat;
import com.project.movie.domain.MovieSlice;
import com.project.movie.domain.Result;
import com.project.movie.service.MoviePriceService;
import com.project.movie.service.MovieSeatService;
import com.project.movie.service.MovieSliceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 影厅座位controller
 * @date 2025/02/06 08:04
 */
@Controller
@ResponseBody
@RequestMapping("seat")
public class MovieSeatController {

    @Autowired
    private MovieSeatService movieSeatService;
    @Autowired
    private MovieSliceService movieSliceService;

    /** 分页获取影厅座位 */
    @PostMapping("getMovieSeatPage")
    public Result getMovieSeatPage(@RequestBody MovieSeat movieSeat) {
        Page<MovieSeat> page = new Page<>(movieSeat.getPageNumber(),movieSeat.getPageSize());
        QueryWrapper<MovieSeat> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(movieSeat.getOfficeId()),MovieSeat::getOfficeId,movieSeat.getOfficeId());
        Page<MovieSeat> movieSeatPage = movieSeatService.page(page, queryWrapper);
        return Result.success(movieSeatPage);
    }

    @PostMapping("getMovieSeatList")
    public Result getMovieSeatList(@RequestBody MovieSeat movieSeat) {
        QueryWrapper<MovieSeat> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(movieSeat.getOfficeId()),MovieSeat::getOfficeId,movieSeat.getOfficeId());
        List<MovieSeat> movieSeatPage = movieSeatService.list(queryWrapper);
        return Result.success(movieSeatPage);
    }

    /** 根据id获取影厅座位 */
    @GetMapping("getMovieSeatById")
    public Result getMovieSeatById(@RequestParam("id")String id) {
        MovieSeat movieSeat = movieSeatService.getById(id);
        return Result.success(movieSeat);
    }

    /** 保存影厅座位 */
    @PostMapping("saveMovieSeat")
    public Result saveMovieSeat(@RequestBody MovieSeat movieSeat) {
        QueryWrapper<MovieSlice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MovieSlice::getOfficeId,movieSeat.getOfficeId());
        int count = movieSliceService.count(queryWrapper);
        if (count > 0) {
            return Result.fail("该影厅已排片,无法新增座位");
        }
        boolean save = movieSeatService.save(movieSeat);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑影厅座位 */
    @PostMapping("editMovieSeat")
    public Result editMovieSeat(@RequestBody MovieSeat movieSeat) {
        QueryWrapper<MovieSlice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MovieSlice::getOfficeId,movieSeat.getOfficeId());
        int count = movieSliceService.count(queryWrapper);
        if (count > 0) {
            return Result.fail("该影厅已排片,无法修改");
        }
        boolean save = movieSeatService.updateById(movieSeat);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除影厅座位 */
    @GetMapping("removeMovieSeat")
    public Result removeMovieSeat(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                QueryWrapper<MovieSlice> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(MovieSlice::getOfficeId,id);
                int count = movieSliceService.count(queryWrapper);
                if (count > 0) {
                    return Result.fail("该影厅已排片,无法删除");
                }
                movieSeatService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("影厅座位id不能为空！");
        }
    }

}
